package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.vo.MybatisReviewVO;
import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
import org.aspectj.weaver.loadtime.Aj;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;


    /**=========================================================新增数据============================================*/
@PostMapping("/saveData")
public AjaxResult saveData(@RequestBody MybatisReviewVO mybatisReviewVO){
    reviewService.saveData(mybatisReviewVO);
    return AjaxResult.success("post增加成功");
}

 @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult save(@PathVariable String name, @PathVariable Integer age, @PathVariable  String sex){
     //return toAjax(reviewService.save(name,age,sex));
//     Review review = new Review();
//     review.setName(name);
//     review.setAge(age);
//     review.setSex(sex);
     reviewService.save(name,age,sex);
return AjaxResult.success("成功插入:1条数");
 }

    /**=========================================================删除数据=============================================*/
@DeleteMapping("/remove/{id}")
    public AjaxResult deldete(@PathVariable("id") Long id){
reviewService.delete(id);
return AjaxResult.success("删除成功");


}
    /**=========================================================修改数据=============================================*/

    @PostMapping("/update")
    public AjaxResult updte(@RequestBody Review review){
        reviewService.update(review);
        return AjaxResult.success("操作成功");
    }

    /**=========================================================查询数据=============================================*/
@GetMapping("/getById")
    public AjaxResult get(@RequestParam("id") Long id  ){

    return AjaxResult.success(reviewService.get(id));
}
@GetMapping("/getDataByPage")
    public TableDataInfo getPage( @RequestParam(required = false, defaultValue = "1") int pageNum,
                               @RequestParam(required = false, defaultValue = "10") int pageSize) {
        startPage();
        List<Review> page = reviewService.getPage();
    ArrayList<MybatisReviewVO> list = new ArrayList<>();
    for (Review review : page) {
        MybatisReviewVO mybatisReviewVO = new MybatisReviewVO();
        BeanUtils.copyProperties(review,mybatisReviewVO);
        list.add(mybatisReviewVO);
    }
        return getDataTable(list);
    }

}
